﻿using KingerRobot.MexicoSailunMDC.Dao;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KingerRobot.MexicoSailunMDC.SpiService.Managers;
internal class SummaryManager
{
    protected readonly Db m_Db;


    public SummaryManager(Db db = null)
    {
        this.m_Db = db ?? Db.Default;
    }

    public int SaveChanges()
    {
        return this.m_Db.SaveChanges();
    }

    public void UpdateCuringRemark(long? curingId, string remark)
    {
        var db = Db.Default;
        var curing = db.RcsEquipCurings.Where(f => f.Id == curingId).FirstOrDefault();

        if (curing is not null)
        {
            curing.Remark = remark;
            db.SaveChanges();
        }

    }

    public void UpdateCuringRemark2(List<long?> curingIds, string remark)
    {
        //var curings = this.m_Db.RcsEquipCurings.Where(f => curingIds.Contains(f.Id)).ToList();
        var db = Db.Default;
        var curings = db.RcsEquipCurings.Where(f => curingIds.Contains(f.Id)).ToList();

        foreach (var curing in curings)
        {
            if (curing is not null)
            {
                curing.Remark = remark;
                db.SaveChanges();
            }
        }

    }

    public void UpdateSummaryRemarksById(long? summaryId, string remark)
    {
        //var summary = this.m_Db.WmsOutOrderSummarys.Where(f => f.Id == summaryId).FirstOrDefault();
        var db = Db.Default;
        var summary = db.WmsOutOrderSummarys.Where(f => f.Id == summaryId).FirstOrDefault();
        if (summary is not null)
        {
            summary.RemarkMemo = remark;
            db.SaveChanges();
        }
    }

    public void SplitSummaryById(long? summaryId, long? splitMasterId)
    {
        var summary = this.m_Db.WmsOutOrderSummarys.Where(f => f.Id == summaryId).FirstOrDefault();

        if (summary is not null)
        {
            summary.MasterId = splitMasterId;
            summary.SplitMasterid = summary.MasterId;
        }
    }

    public void UpdateSummaryStartLocAndRealCount(long? summaryId, long? startLoc)
    {
        var summary = this.m_Db.WmsOutOrderSummarys.Where(f => f.Id == summaryId).FirstOrDefault();

        if (summary is not null)
        {
            summary.StartLoc = startLoc;
            summary.RealCount = 1;
        }
    }

    public POWmsOutOrderMaster AddOutMasters(int orderType, int priority = 10)
    {
        var db = Dao.Db.Default;
        var orderId = XID.Nextval;

        var bean = new POWmsOutOrderMaster()
        {
            Id = orderId,
            OrderCode = K8.LSF.Client.CodeValue(new DateTime(), "OutOrder"),
            OrderType = orderType,
            OrderStatus = (int)EnumCodeOutOrderStatus.Create,
            OrderPriority = priority,
        };
        //this.m_Db.WmsOutOrderMasters.Add(bean);
        db.WmsOutOrderMasters.Insert(bean);
        db.SaveChanges();

        return bean;
    }
    public int  AddOutOrderSummary(POWmsOutOrderSummary pOWmsOutOrderSummary)
    {  
        var db = Dao.Db.Default;
        var result = db.WmsOutOrderSummarys.Insert(new POWmsOutOrderSummary()
        {
            Id = pOWmsOutOrderSummary.Id,
            MasterId = pOWmsOutOrderSummary.MasterId,
            SummaryIndex = pOWmsOutOrderSummary.SummaryIndex,
            TargetLoc = pOWmsOutOrderSummary.TargetLoc,
            MaterialId = pOWmsOutOrderSummary.MaterialId,
            SkuBatch = pOWmsOutOrderSummary.SkuBatch,
            Serno = pOWmsOutOrderSummary.Serno,
            Stock = pOWmsOutOrderSummary.Stock,
            RotateAngle = pOWmsOutOrderSummary.RotateAngle,
            RemarkMemo= pOWmsOutOrderSummary.RemarkMemo,
            //TbmPs = pOWmsOutOrderSummary.TbmPs,
            //CuringSpec = pOWmsOutOrderSummary.CuringSpec,
            //CuringSpecDesc = pOWmsOutOrderSummary.CuringSpecDesc,
        });
        db.SaveChanges();
        return result;
    }

    public void AddOutOrderSummarys(List<POWmsOutOrderSummary> pOWmsOutOrderSummarys)
    {
        var db = Dao.Db.Default;

        foreach (var item in pOWmsOutOrderSummarys)
        {
            db.WmsOutOrderSummarys.Insert(new POWmsOutOrderSummary()
            {
                Id = item.Id,
                MasterId = item.MasterId,
                SummaryIndex = item.SummaryIndex,
                TargetLoc = item.TargetLoc,
                MaterialId = item.MaterialId,
                SkuBatch = item.SkuBatch,
                Serno = item.Serno,
                Stock = item.Stock,
                RotateAngle = item.RotateAngle,
                //TbmPs = item.TbmPs,
                //CuringSpec = item.CuringSpec,
                //CuringSpecDesc = item.CuringSpecDesc,
            });
        }
        db.SaveChanges();
    }
}
